<template>
  <div class="card-items">
    <div class="head-part">
      <div class="title">
        <img src="@/assets/van-img.png" alt="" />
        <span>{{ title }}</span>
      </div>
      <van-popover
        v-model:show="showPopover"
        :actions="actions"
        @select="onSelect"
      >
        <template #reference>
          <div class="qs-text">
            {{ selectName }} <van-icon name="arrow-down" />
          </div>
        </template>
      </van-popover>
    </div>
    <div class="tab-btn">
      <span
        v-for="(item, index) in tabList"
        :key="index"
        @click="tabClick(index)"
        :class="tabIndex == index ? 'btn-select' : 'btn-unselect'"
        >{{ item.name }}</span
      >
    </div>
    <div class="desc-panel">
      <div class="left-desc">
        <span class="text1">{{ title2 }}</span>
        <span class="text2">{{ monthSatisfaction }}</span>
        <span class="text1" v-if="isRank == true">排名</span>
        <span class="text2" v-if="isRank == true">{{ ranking }}</span>
      </div>
      <div class="right-desc">
        环比<span :style="{ color: isNegative(hb) ? '#e34343' : '#10cfa5' }">{{
          hb
        }}</span>
        <img v-if="isNegative(hb)" src="@/assets/pan-view/up(1).png" />
        <img v-else src="@/assets/pan-view/up.png" />
      </div>
    </div>
    <lineEcharts :chartObj="chartObj" :cindex="index1" />
  </div>
</template>
<script>
import { reactive, toRefs, onMounted } from "vue";
import lineEcharts from "@/components/lineEcharts.vue";
export default {
  components: {
    lineEcharts,
  },
  props: ["itemObj"],
  setup(props, { emit }) {
    let itemObj = props.itemObj;
    const state = reactive({
      selectName: "全省趋势",
      actions: [{ text: "全省趋势" }, { text: "分公司排名" }],
      showPopover: false,
      hb: "-0.18%",
      title: itemObj.title,
      title2: itemObj.title2,
      isRank: itemObj.isRank,
      dataItem: itemObj,
      tabList: props.itemObj.tabList,
      index1: itemObj.index1,
      tabIndex: 0,
      monthSatisfaction: "92.24%",
      ranking: "24",
      chartObj: itemObj.chartObj,
    });

    //判断是否为负数
    function isNegative(num) {
      if (!num) return true;
      num = num + "";
      if (num.indexOf("-") != -1 && num != "-") return true;
      return false;
    }
    //一级tab切换
    function tabClick(val) {
      state.tabIndex = val;
      emit("changeMsg", state.tabList[val]);
      if (val == 0) {
        state.monthSatisfaction = "92.24%";
        state.ranking = "24";
      } else {
        state.monthSatisfaction = "96.24%";
        state.ranking = "10";
      }
    }
    const onSelect = (action) => {
      state.selectName = action.text;
      if (state.selectName == "分公司排名") {
        state.index1 = itemObj.index1 + 1000;
        state.dataItem.chartObj.xAxisData = [
          "南京",
          "泰州",
          "无锡",
          "南通",
          "苏州",
          "盐城",
          "扬州",
          "淮安",
          "常州",
          "宿迁",
          "镇江",
          "徐州",
          "连云港",
        ];
        state.chartObj = {
          iscity: true,
          ...state.dataItem.chartObj,
        };
      } else {
        state.dataItem.chartObj.xAxisData = [
          "10/01",
          "10/02",
          "10/03",
          "10/04",
          "10/05",
          "10/06",
          "10/07",
          "10/08",
          "10/09",
          "10/10",
        ];
        state.index1 = itemObj.index1 + 10002;
        state.chartObj = {
          iscity: false,
          ...state.dataItem.chartObj,
        };
      }
    };
    onMounted(() => {});
    return {
      ...toRefs(state),
      tabClick,
      onSelect,
      isNegative,
    };
  },
};
</script>
<style lang="scss" scoped>
.card-items {
  margin: 10px;
  width: calc(100% - 20px);
  // height: 298px;
  background-color: #fff;
  border-radius: 5px;
  padding: 10px 0 2px 10px;
  margin-bottom: 10px;
  .head-part {
    width: 100%;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    .qs-text {
      padding: 0 15px;
      line-height: 24px;
      color: #666666;
      margin-right: 10px;
      font-size: 12px;
      background: #ffffff;
      box-shadow: 0px 0px 2px 0px rgba(0, 0, 0, 0.2);
      border-radius: 4px;
    }
  }
  .title {
    width: 50%;
    display: flex;
    align-items: center;
    span {
      font-size: 16px;
      font-weight: 400;
      color: #333333;
    }
  }
  .tab-btn {
    width: calc(100% - 8px);
    display: flex;
    flex-direction: row;
    align-items: center;
    margin: 8px 0 0 8px;
    overflow-x: auto;
    .btn-select {
      padding: 6px 13px;
      background: linear-gradient(285deg, #fe6800 0%, #ff9900 100%);
      border-radius: 12px;
      color: #ffffff;
      white-space: nowrap;
      font-size: 13px;
      margin-right: 4px;
    }
    .btn-unselect {
      padding: 6px 13px;
      background: #f3f3f3;
      border-radius: 12px;
      color: #333333;
      white-space: nowrap;
      font-size: 13px;
      margin-right: 4px;
    }
  }

  .tab-btn::-webkit-scrollbar {
    width: 0;
    height: 0;
  }
  .desc-panel {
    width: 100%;
    margin-top: 20px;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    .left-desc {
      font-size: 14px;
      .text1 {
        font-size: 14px;
        font-weight: 400;
        color: #333333;
        margin-right: 6px;
      }
      .text2 {
        font-size: 21px;
        font-weight: bold;
        color: #333333;
        margin-right: 6px;
      }
    }
    .right-desc {
      padding: 0 10px;
      height: 24px;
      width: 100px;
      line-height: 24px;
      margin-right: 10px;
      background: #fff0dc;
      color: #333333;
      border-radius: 4px;
      font-size: 12px;
      text-align: center;
    }
  }
}
</style>
